<?php

class Caching{
	
	private $allowCache;
	static $cache;
	static $config;
	
	static function init(){
		self::startCache();
		self::$config=Zend_Registry::get('config_site');
	}
	
	static function save($data, $key, $tag){
		if(intval(self::$config['allowCache'])){
			return self::$cache->save($data, $key, array($tag));
		}
		return false;
	}
	
	static function load($key){
		if(intval(self::$config['allowCache'])){
			return self::$cache->load($key);
		}
		return false;
	}
	
	static function remove($tag){
		if(intval(self::$config['allowCache'])){
    		self::$cache->clean(
          	 Zend_Cache::CLEANING_MODE_MATCHING_TAG,
         	 array($tag)
      		);			
		}
		return false;
	}
	
	static function clean(){
		if(intval(self::$config['allowCache'])){
			self::$cache->clean(Zend_Cache::CLEANING_MODE_ALL);
		}
	}
	
	private function startCache(){
   
	     $frontendOptions = array(
	         'lifetime' => 2*24*60*60, // cache lifetime of 2 day
	         'automatic_serialization' => true
	     );
	   
	     $backendOptions = array(
	          'cache_dir' => CACHE_DIR  // Directory where to put the cache files
	     );
	      // getting a Zend_Cache_Core object
	     self::$cache = Zend_Cache::factory('Core',
	                                   'File',
	                                   $frontendOptions,
	                                   $backendOptions);
	}
}

?>